package com.tcy.bigevent.controller;

import com.tcy.bigevent.pojo.Article;
import com.tcy.bigevent.pojo.PageBean;
import com.tcy.bigevent.pojo.Result;

import com.tcy.bigevent.service.ArticleService;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

//    @GetMapping("/list")
//    public Result<String> list(@RequestHeader(name="Authorization") String token, HttpServletResponse  response){
//        return Result.success("所有的文章数据");
//        //验证token
//        try{
//            Map<String, Object> claims = JwtUtil.parseToken(token);
//            return Result.success("所有的文章数据");
//        } catch (Exception e){
//            //http响应状态码为401
//            response.setStatus(401);
//            return Result.error("未登录");
//        }
//    }
    //文章列表（条件分页）
    @GetMapping
    public Result<PageBean<Article>> list(@RequestParam Integer pageNum,
                                          @RequestParam Integer pageSize,
                                          @RequestParam(required = false) Integer categoryId,
                                          @RequestParam(required = false) String state){
        System.out.println("pageNum:"+pageNum+",pageSize:"+pageSize+",categoryId:"+categoryId+",state:"+state);
        return Result.success(articleService.list(pageNum,pageSize,categoryId,state));
    }
    @PostMapping
    public Result add(@RequestBody @Validated(Article.Add.class) Article article){
        articleService.add(article);
        return Result.success("添加成功");
    }
    @PutMapping
    public Result update(@RequestBody @Validated(Article.Update.class) Article article){
        articleService.update(article);
        return Result.success("修改成功");
    }

    @GetMapping("/detail")
    public Result<Article> detail(@RequestParam Integer id){
        return Result.success(articleService.getById(id));
    }

    @DeleteMapping
    public Result delete(@RequestParam Integer id){
        articleService.delete(id);
        return Result.success("删除成功");
    }
}
